<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta HTTP-EQUIV="pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <meta HTTP-EQUIV="expires" CONTENT="0">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>角色管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/ui.css" rel="stylesheet">
    <link href="css/control.css" rel="stylesheet" >
    <link href="css/layui.css" rel="stylesheet">
    <style>
        #body {
            width: 100%;
            max-height: 100%;
            overflow-y: scroll;
            overflow-x: hidden;
        }

        .articleImg {
            /* width: 100%; */
            /* height: auto !important; */
            max-width: 100px;
        }

        th,
        td {
            text-align: center;
        }
        .form-group-self {
            margin-top: 15px;
            margin-bottom: 15px;
        }
    </style>
</head>

<body class="gray-bg animated fadeInRight">
    <div id="body">
        <div class="wrapper wrapper-content">
            <div class="row">
                <div class="col-sm-12">
                    <div class="ibox float-e-margins">

                        <div class="form-group-self">
                            <label class="col-xs-3 control-label" style="width: 75px;margin-top: 5px;">应用：</label>
                            <div class="layui-input-inline layui-form">
                                <select name="appType" lay-filter="app">
                                    <option value="0" selected="">后台管理</option>
                                    <option value="1" >app客户端</option>
                                </select>
                            </div>
                            <div class="fr" style="">
                                <a href="addRole.html" class="btn btn-info btn-sm" title="添加角色">添加角色</a>
                            </div>
                        </div>

                        <div class="ibox-content">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th width="8%">角色名称</th>
                                        <th width="20%">权限列表</th>
                                        <th width="6%">应用名称</th>
                                        <th width="6%">操作时间</th>
                                        <th width="6%">操作</th>
                                    </tr>
                                </thead>
                                <tbody id="list">
                                </tbody>
                            </table>
                        </div>
                        <!-- 底部分页-->
                        <!--<div id="pageRow" class="fixBottom">-->
                            <!--<div class="fr" id="page">-->
                                <!--<ul class="pagination" style="margin:0;float:right;">-->
                                    <!--<li class="paginate_button" id="firstPage">-->
                                        <!--<a href="javascript:">-->
                                            <!--<<</a>-->
                                    <!--</li>-->
                                    <!--<li class="paginate_button" id="previousPage">-->
                                        <!--<a href="javascript:">-->
                                            <!--<</a>-->
                                    <!--</li>-->
                                    <!--<li class="paginate_button">-->
                                        <!--<a href="javascript:" style="padding:0">-->
                                            <!--<select class="" name="" style="padding:4px 10px" id="pageInfo">-->
                                            <!--</select>-->
                                        <!--</a>-->
                                    <!--</li>-->
                                    <!--<li class="paginate_button" id="nextPage">-->
                                        <!--<a href="javascript:">></a>-->
                                    <!--</li>-->
                                    <!--<li class="paginate_button" id="lastPage">-->
                                        <!--<a href="javascript:">>></a>-->
                                    <!--</li>-->
                                <!--</ul>-->
                            <!--</div>-->

                        <!--</div>-->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 全局js -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/work/common.js"></script>
    <script src="js/plugins/laydate/laydate.js"></script>
    <script src="js/plugins/layer/layer.min.js"></script>
    <script src="js/plugins/layer/layer.js"></script>
    <!-- 自定义js -->
    <script src="js/content.js"></script>
    <script src="js/layui.js"></script>
    <script>

        var dataList;
        var selectAppKey;
        var form;

        // layui表单使用
        layui.use(['form'], function(){
            form = layui.form;
            form.on('select(app)', function (data) {
                if (data.value == 0) {          // 选择的是后台
                    selectAppKey = appKey;
                    initAppData();
                } else if (data.value == 1) {   // 选择的是app
                    selectAppKey = androidAppKey;
                    initAppData();
                }
            });
        });

        $(document).ready(function () {
            selectAppKey = appKey;
            initAppData();

        });

        // 获取app应用信息
        function initAppData() {
            // 应用程序的appKey
            initPermissionList(selectAppKey);
        }

        // 获取角色权限列表
        function initPermissionList(appKey) {
            ajax({
                url: "permission/getRolePermissionList",
                data: {appKey:appKey},
                successx: function (data) {
                    if (data.success) {
                        creatList(data.data);
                    } else {
                        layer.msg(data.message)
                    }
                }
            });
        }

        /**
         * 创建表单数据
         * @param data
         */
        function creatList(data) {
            dataList = data;
            var tpl = '';
            for (var i = 0; i < data.length; i++) {
                tpl += '<tr class="viewDb" data-i="'+i+'" data-val="' + data[i].roleId + '">' +
                    '<td>' + data[i].roleNameZh + '</td>' +
                    '<td>' + data[i].permissions + '</td>' +
                    '<td>' + data[i].appName +'</td>' +
                    '<td>' + timeStamp2String(data[i].updateTime, "ymdhms") +'</td>' +
                    '<td>' +
                    '<a class="checkInfo btn btn-info btn-xs">权限分配</a>&nbsp;&nbsp;' +
                    '<a class="updateRole btn btn-success btn-xs">修改</a>&nbsp;&nbsp;' +
                    '<a class="delRole btn btn-danger btn-xs">删除</a></td>' +
                    '</tr>'
            }
            $("#list").empty().append(tpl);

            $('.checkInfo').click(function () {
                var position = $(this).parent().parent().attr("data-i");
                setStorage("roleId", dataList[position].roleId);
                setStorage("roleName", dataList[position].roleNameZh);
                setStorage("appKey", selectAppKey);
                layer.open({
                    type: 2,
                    title: '编辑权限',
                    maxmin: true,
                    shadeClose: false, //点击遮罩关闭层
                    closeBtn: 1,
                    maxmin: false,
                    area: ['800px', '600px'], // 长，高
                    content: 'changePermission.html'
                });
            });


            // 修改角色
            $('.updateRole').click(function () {
                var position = $(this).parent().parent().attr("data-i");
                var roleInfo = {
                    roleId: dataList[position].roleId,
                    roleName: dataList[position].roleName,
                    roleNameZh: dataList[position].roleNameZh
                };
                setStorJson("roleJson", roleInfo);
                layer.open({
                    type: 2,
                    title: '修改角色',
                    maxmin: true,
                    shadeClose: false, //点击遮罩关闭层
                    closeBtn: 1,
                    maxmin: false,
                    area: ['470px', '300px'], // 长，高
                    content: 'aEditRole.html'
                });
            });

            /**
             * 删除角色
             */
            $('.delRole').click(function () {
                var position = $(this).parent().parent().attr("data-i");
                var roleId = dataList[position].roleId;
                layer.confirm('您确定要删除吗？删除后不可恢复', {
                    btn: ['确定','取消'] //按钮
                }, function(){

                    layer.close(layer.index);
                    ajaxPost({
                        url: "role/deleteRoleInfo",
                        data: {
                            appKey:selectAppKey,
                            roleId:roleId
                        },
                        successx: function (data) {
                            if (data.success) {
                                initAppData();
                            } else {
                                layer.msg(data.message)
                            }
                        }
                    });
                }, function(){
                    layer.close(layer.index);
                });

            });


        }
    </script>
</body>

</html>